package clientapp;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Date;
import java.text.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.text.DecimalFormat;
import java.util.StringTokenizer;

public class rentMovie extends JFrame implements ActionListener
{
	private int Count = 0;
	
	private JLabel lblTel = new JLabel("Select Number:");
	private JLabel lblDetails = new JLabel("Rented details:");
	private JLabel lblSelect = new JLabel("Select a Movie:");
	private JLabel lblHeader = new JLabel("RENT MOVIE");
	
	private String numbers[];
	private String movies[];
   	
	private JComboBox custBox; 
	private JComboBox movBox;
	private JTextArea rentDetails = new JTextArea(20,30);
	
	private JRadioButton dummy = new JRadioButton();
	
	private JButton btnCancel = new JButton("Exit");
	private JButton btnRent = new JButton("Rent");
	private JButton btnMore = new JButton("Rent again...");
	
	private ButtonGroup rndGroup = new ButtonGroup();
	
	//Panels
	private JPanel southPanel = new JPanel();
	private JPanel sPanel = new JPanel();
	private JPanel topPanel = new JPanel();
	private JPanel numPanel = new JPanel();
	private JPanel movPanel = new JPanel();
	private JPanel topCenPanel = new JPanel();
	private JPanel bottomPanel = new JPanel();

	private JPanel centerPanel = new JPanel();
	private JPanel northPanel = new JPanel();

	private boolean displayed = false;
	private String categoryName = "";
	private String telNumber = " ";
	private String returnDate = " ";
	private String selectedMovie = " ";
	private boolean out = false;
	
	
	Date currentDate = new Date();
	SimpleDateFormat myDay = new SimpleDateFormat("dd");
	String DD = myDay.format(currentDate);
	SimpleDateFormat myMonth = new SimpleDateFormat("MM");
	String MM = myMonth.format(currentDate);
	SimpleDateFormat myYear = new SimpleDateFormat("yyyy");
	String YY = myYear.format(currentDate);
	
	private String number = new String();
	private String Movie = new String();
	private String code = new String();

    private Client clientr = new Client();
			
	public rentMovie(Client clientp)
	{
		super("Rent Movie");
		clientr = (Client)clientp;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(420,320);
		setResizable(false);
		addWindowListener (new WindowAdapter ()
		{
	    	public void windowClosing (WindowEvent e)
	    	{ //overiding windowClosing
				runStore.store.setEnabled(true);
	    	}
		} //the improved WindowListener is added.
		);
		BorderLayout main = new BorderLayout(5,5);
		setLayout(main);
		setLocationRelativeTo(null);
				
		//Client.sendQuery("RentMovieF1");
		LoadNumbers();
		
		clientr.sendQuery("RentMovieF2");
		LoadComboBox();
			
		
		rentDetails.setEditable(false);
		btnMore.setEnabled(false);
		 
		northPanel.setLayout(new FlowLayout());
		topPanel.setLayout( new GridLayout(2,2,5,5));
		bottomPanel.setLayout( new GridLayout(1,1));
		 
		northPanel.add(lblHeader);
		numPanel.add(lblTel);
		numPanel.add(custBox);
		movPanel.add(lblSelect);
		movPanel.add(movBox);
		 
		topPanel.add(numPanel);
		topPanel.add(movPanel);
		 
		topCenPanel.add(topPanel);
		bottomPanel.add(rentDetails);
		 
		 // add to center Panel.
		centerPanel.add(topCenPanel);
		centerPanel.add(bottomPanel);
		 
		 //build south panel.
		southPanel.add(btnRent);
		southPanel.add(btnMore);
		southPanel.add(btnCancel);
		 
		sPanel.add(southPanel);
		 
		 //display Panels.
		add(northPanel, BorderLayout.NORTH);
		add(centerPanel, BorderLayout.CENTER);
		add(sPanel, BorderLayout.SOUTH);
		 		
		btnCancel.addActionListener(this);
		btnRent.addActionListener(this);
		btnMore.addActionListener(this);
		
		setVisible(true);
	}
	
	public void LoadComboBox()
	{
		movies = new String[ clientr.getResult().length];
			
		for (int i = 0; i < clientr.getResult().length; i++)
		{
			for(int j = 0; j < 2; j++)
			{
				code = clientr.getResult()[i][0];
				Movie = clientr.getResult()[i][1];
			}
			String movieToRent = code + " " + Movie;
			movies[i] =  movieToRent;
		}
		movBox = new JComboBox(movies);
	}
	
	public void LoadNumbers()
	{
		numbers = new String[ clientr.getResult().length];
		
		for (int i = 0; i < clientr.getResult().length; i++)
		{
			for(int j = 0; j < 1; j++)
			{
				number = clientr.getResult()[i][0];
			}
		
			numbers[i] =  number;
		}
		custBox = new JComboBox(numbers);		
	}
	
	
	public void actionPerformed(ActionEvent e)
	{
		
		if(e.getSource() == btnCancel)
		{
			int reply = JOptionPane.showConfirmDialog(null, "Are you show you want to\n   close the rent movie form",
				"Exit Application", JOptionPane.YES_NO_OPTION);
			if(reply == JOptionPane.YES_OPTION)
			{
				//runStore.store.setEnabled(true);	
				dispose();
			}
		}
		else
		if(e.getSource() == btnRent)
		{
			boolean haveCredit = false;
			double latestCredit = 0;
			int retDD = Integer.parseInt(DD);
			int retMM = Integer.parseInt(MM);
			int retYY = Integer.parseInt(YY);
			
			int Months[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
			retDD++;
			
			// start of Date checking			
			if (((retYY%4) == 0) && (retMM == 2) && (retDD > 28))
			{
				Months[1]++;
			}
				
			if (retDD > Months[retMM-1])
			{
				if (!(retMM == 12)) // Not December
				{
					retMM++;
				}
				else // New Year
				{
					retMM = 1;
					retYY++;
				}
				retDD = 1;
			}
			// end of Date checking
			
			DecimalFormat date = new DecimalFormat("00");
			
			String custNumber = (String)custBox.getSelectedItem();
			String selectedDVDCode = (String)movBox.getSelectedItem();
			StringTokenizer st = new StringTokenizer(selectedDVDCode);
			//used to seperate DVDCode from Title in combo box selected movie
			selectedDVDCode = st.nextToken();
				
			String sendToServer = "RentMovie#" + custNumber + "#" + selectedDVDCode;
			clientr.sendQuery(sendToServer);
			
			//assess server message returned
			if(clientr.getResult(0,0).equals("#Movie rented out"))
			{
				returnDate = date.format(retDD) + "/" + date.format(retMM) +"/" + retYY;
				rentDetails.setText("RENTED DETAILS:"  + "\n");
				rentDetails.append(" " + "\n");
				rentDetails.append("Movie:\t" + st.nextToken() + "\n");
				//rentDetails.append("Category:\t" + Dvd.DvdList[movIndex].getCategory() + "\n");
			    rentDetails.append("Cust Tel No:\t" + custNumber +"\n");
				rentDetails.append("Rent Date:\t" + DD +"/"+ MM + "/" + YY + "\n");
				rentDetails.append("Return Date:\t" + returnDate);
				btnMore.setEnabled(true);
				btnRent.setEnabled(false);
				custBox.setEnabled(false);
				movBox.setEnabled(false);
				
				movBox.removeItem((String)movBox.getSelectedItem());
				if(movBox.getItemCount()==0)
				{
					JOptionPane.showMessageDialog(null, "There are no more DVD's to be rented\nThis form will now close");
					dispose();
				}
			}
			else //Customer has insufficient credit
				JOptionPane.showMessageDialog(null, clientr.getResult(0,0), "Transaction Failed", JOptionPane.WARNING_MESSAGE);
		}
		
		else if(e.getSource() == btnMore)
		{
			int Question = JOptionPane.showConfirmDialog(null, "Are you sure you want to rent more movies", "Rent Movie", JOptionPane.YES_NO_OPTION);
			
			if(Question == JOptionPane.YES_OPTION)
			{
				//Clear all fields.
				movBox.setEnabled(true);
				rentDetails.setText("");
				btnMore.setEnabled(false);
			    btnRent.setEnabled(true);
			    custBox.setEnabled(true);
			}
			else
			{
			//	runStore.store.setEnabled(true);
				dispose();
			}
		}
	}
}
	
